﻿using SQLServer.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Tenancy.Helper;

namespace Tenancy.Server
{
    /// <summary>
    /// Hbung_UpdateComInfo 的摘要说明
    /// </summary>
    public class Hbung_UpdateComInfo : IHttpHandler
    {
        /// <summary>
        /// 用户修改个人资料接口
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //用户登录的手机号
            string Hbung_Tel = context.Request.Params["Tel"];
            string Hbung_Phone = string.Empty;
            if (string.IsNullOrEmpty(Hbung_Tel))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Tel为空"));
            }
            else
            {
                string pattern = "^1[0-9]{10}$"; //正则表达式字符串
                Regex cgsregex = new Regex(pattern);
                Hbung_Phone = DESEncrypt.MD5Decrypt(Hbung_Tel);
                bool isMatch = cgsregex.IsMatch(Hbung_Phone);
                if (!isMatch)
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Tel格式不正确"));
                    return;
                }
            }
            //签名
            string HBsign = context.Request.Params["Sign"];
            if (string.IsNullOrEmpty(HBsign))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Sign为空"));
                return;
            }
            else
            {
                if (GenerClass.IsOkMatch(Hbung_Phone) != Regex.Replace(HBsign, @"\s", "+"))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Sign不匹配"));
                    return;
                }
            }

            //公司名字
            string Hb_ComName=context.Request.Params["ComName"];
            if(string.IsNullOrEmpty(Hb_ComName))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("ComName为空"));
                return;
            }
            else
            {
                if(Hb_ComName.Length>50)
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("ComName长度太长"));
                    return;
                }
            }
            //公司地址
            string Hb_Address = context.Request.Params["AddRess"];
            if (string.IsNullOrEmpty(Hb_Address))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("AddRess为空"));
                return;
            }
            else
            {
                if (Hb_Address.Length > 50)
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("AddRess长度太长"));
                    return;
                }
            }
            //联系人
            string Hb_Name = context.Request.Params["Name"];
            if (string.IsNullOrEmpty(Hb_Name))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Name为空"));
                return;
            }
            else
            {
                if (Hb_Name.Length > 50)
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Name长度太长"));
                    return;
                }
            }
            //固定电话 
            string Hb_Phone = context.Request.Params["Phone"];
            if (string.IsNullOrEmpty(Hb_Phone))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Phone为空"));
                return;
            }
            else
            {
                if (Hb_Phone.Length > 50)
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Phone长度太长"));
                    return;
                }
            }
           

            string infostr = string.Format(@"update Hbung_CompanyInfo set CompanyName='{0}',Phone='{1}',Address='{2}',Name='{3}' where CellTel='{4}' 
               ", Hb_ComName, Hb_Phone, Hb_Address, Hb_Name, Hbung_Phone);
            if(DbLocalSQL.ExecuteSql(infostr)>0)
            {
                string onestr = string.Format("select ID,CompanyName,Phone,Address,PicUrl,Name,CellTel,CraeatTime from Hbung_CompanyInfo where CellTel='{0}' ", Hbung_Phone);
                DataTable HB_ComInfo = DBHelpSQL.Query(onestr).Tables[0];
                string Hb_DateJson = JsonHelper.DataTableToJSON(HB_ComInfo);
                context.Response.Write(GenerClass.SetSucessJsData("修改成功", Hb_DateJson));
            }
            else
            {
                context.Response.Write(GenerClass.SetChangeErrorJsData("修改失败"));
                return;
            }
           
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}